<?php $this->headTitle(_mytranslator('Permissions')); ?>
<form action="javascript:void(0);" method="post" id="permissionform">
    <p>
        <label name=""><?php echo _mytranslator('Select Module'); ?></label>
  
        <select name="module" id="module">
            <option value="">-Select-</option>
            <?php foreach ($this->modules as $module): ?>
                <option value="<?php echo $module['resource_module']; ?>"><?php echo $module['resource_module']; ?></option>
            <?php endforeach; ?>
        </select>
    </p>
        <br />
        <p>
        <label><?php echo _mytranslator('Role'); ?></label>
        <select name="role">
            <option value="">-Select-</option>
            <?php foreach ($this->roles as $key => $role): ?>
                <?php if ($role['role_name'] != 'root'): ?>
                    <option value="<?php echo $role['id']; ?>" selected="selected"><?php echo $role['role_name']; ?></option>
                <?php endif; ?>
            <?php endforeach; ?>
        </select>   
        </p>
        <br />
        <p>
        <label><?php echo _mytranslator('Select Resource'); ?></label>
        <select id="resource" name="resource">
            <option value="">-Select-</option>
        </select>
        </p>
        <br />
        <br />
        <table>
            <tr>
                <td width="225"><?php echo _mytranslator('Permission Available'); ?></td>
                <td width="50">&nbsp;</td>
                <td width="225"><?php echo _mytranslator('Permissions Assigned'); ?></td>
            </tr>
            <tr>
                <td><select style="min-height: 200px;" multiple="multiple" id="availableperm" name="availableperm[]">
                        <option value="">-Select-</option>
                    </select>
                    <br/></td>
                <td style="vertical-align:middle;"><input type="button"  value=">>" id="addperm"/>
                    <br/><br />
                    <input type="button"  value="<<" id="deleteperm"/>
                </td>
                <td><select style="min-height: 200px;" name="assignedperm[]" multiple="multiple" id="assignedperm" >
                        <option value="">-Select-</option>
                    </select></td>
            </tr>
        </table>
</form>

<script>
    $(document).ready(function(){
        
			
        $("#module").change(function(){
            var module = $(this).val();
            
            $.post("/admin/aclpermission/index/op/resource",{mod:module},function(data){
            
                var datas =  eval(data);
                var html = '<option value="">-select-</option>';
                for(var i=0;i<datas.length;i++)
                {
                    html+='<option value="'+datas[i]['resource']+'::'+datas[i]['id']+'">'+datas[i]['resource']+'</option>';
                }    
                $("#resource").html(html);
            
            });
            
        })
        
        $("#resource").change(function(){
            
            var resource = $(this).val();
            var modulename = $("#module").val();
            $.post("/admin/aclpermission/index/op/permission",{resource:resource,modules:modulename},function(data){
                
             
                var data = ($.JSON.decode(data));
                var html = '';
                var avhtml = '';
                for(var i=0;i<data['assigned'].length;i++)
                {
                    avhtml+='<option value="'+data['assigned'][i]['permission']+'">'+data['assigned'][i]['permission']+'</option>';
                }    
                $("#assignedperm").html(avhtml);    
			
			
                for(var i=0;i<data['available'].length;i++)
                {
                    html+='<option value="'+data['available'][i]+'">'+data['available'][i]+'</option>';
                }    
                $("#availableperm").html(html);
                 
                $("#assignedperm option").each(function(){
                    var text = $(this).text();
                    $("#availableperm option").each(function(){
                        if($(this).text() == text)
                        {
                            $(this).remove();
							
                        }
                    })
                })    
        
                
            })
			
		
			
            
        })
        
        $("#addperm").click(function(){
            if($("#availableperm option:selected").length >0)
            {
                $("#availableperm option:selected").each(function(){
                    var html = '<option value="'+$(this).text()+'">'+$(this).text();+'<option>';
                    $("#assignedperm").append(html);
                    $(this).hide();
                })
                $.post("/admin/aclpermission/add",$("#permissionform").serialize(),function(data){
				$(this).remove();
                });	
				
            }else return false;
        })
    	
        $("#deleteperm").click(function(){
            if($("#assignedperm option:selected").length >0)
            {
                $("#assignedperm option:selected").each(function(){
                    var html = '<option value="'+$(this).text()+'">'+$(this).text();+'<option>';
                    $("#availableperm").append(html);
                    $(this).hide();
                })
				
                $.post("/admin/aclpermission/delete",$("#permissionform").serialize(),function(data){
				$(this).remove();		
                });
				
            }else return false;
			
        })
        
	
        
    })
    
</script>   
